package cn.e_eap.esite.manager.impl;

import cn.e_eap.esite.bean.Article;
import cn.e_eap.esite.manager.ArticleManager;
import cn.e_eap.esite.manager.BaseManager;
import cn.e_eap.esite.web.form.PagerForm;
import java.util.List;

public class ArticleManagerImpl extends BaseManager<Article> implements
        ArticleManager {

    @Override
    public void add(Article article) {
        article.setSubmitTime(System.currentTimeMillis());
        super.add(article);
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(System.currentTimeMillis());
        super.update(article);
    }

    @Override
    public void delete(String ids) {
        super.removeByIds(Article.class, ids);
    }

    @Override
    public List<Article> find(Article condition, PagerForm pf) {
        StringBuilder hql = new StringBuilder();
        if (condition != null) {
            if (condition.getTitle() != null
                    && condition.getTitle().length() > 0) {
                hql.append(" and @.title like '%").append(condition.getTitle()).append("%'");
            }

            if (condition.getTitle2() != null
                    && condition.getTitle2().length() > 0) {
                hql.append(" and @.title2 like '%").append(condition.getTitle2()).append("%'");
            }

            if (condition.getDescription() != null
                    && condition.getDescription().length() > 0) {
                hql.append(" and @.description like '%").append(condition.getDescription()).append("%'");
            }

            if (condition.getType() != 0) {
                hql.append(" and @.type=").append(condition.getType());
            }

            if (condition.getType() != 0) {
                hql.append(" and @.type=").append(condition.getType());
            }

            if (condition.getFlag() != null && condition.getFlag().length() > 0) {
                hql.append(" and @.type='").append(condition.getType()).append("'");
            }

        }

        return super.getPageModel(Article.class, hql.toString(), pf);
    }

    @Override
    public Article findById(int id) {
        return getHibernateTemplate().get(Article.class, id);
    }
}
